

#include <sys/atomic.h>

#ifdef __HAVE_ATOMIC64_OPS

uint64_t fetch_and_nand_8(volatile uint64_t *, uint64_t, ...)
    asm("__sync_fetch_and_nand_8");
uint64_t nand_and_fetch_8(volatile uint64_t *, uint64_t, ...)
    asm("__sync_nand_and_fetch_8");

uint64_t
fetch_and_nand_8(volatile uint64_t *addr, uint64_t val, ...)
{
	uint64_t old, new;

	do {
		old = *addr;
		new = ~(old & val);
	} while (atomic_cas_64(addr, old, new) != old);
	return old;
}

uint64_t
nand_and_fetch_8(volatile uint64_t *addr, uint64_t val, ...)
{
	uint64_t old, new;

	do {
		old = *addr;
		new = ~(old & val);
	} while (atomic_cas_64(addr, old, new) != old);
	return new;
}

#endif
